package com.spring.shiro.controller;


import com.spring.shiro.constant.Constant;
import com.spring.shiro.dao.SsoLoginLogMapper;
import com.spring.shiro.dao.UserMapper;
import com.spring.shiro.model.ShiroUser;
import com.spring.shiro.util.LinTools;
import com.spring.shiro.util.MD5Util;
import com.spring.shiro.util.Userinfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private LinTools linTools;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private SsoLoginLogMapper ssoLoginLogMapper;

	@RequestMapping(value = "/login", method = { RequestMethod.POST })
	public String login(HttpServletRequest request, ShiroUser user) {
		Subject subject = SecurityUtils.getSubject();
		user.setPassword(MD5Util.encrypt(user.getPassword()));
		if (linTools.getKaptchaSwich()) {
			String kaptchaRecevied = request.getParameter("kaptcha");
			// 用户输入的验证码的值
			String kaptchaExpected = (String) request.getSession()
					.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
			// 校验验证码是否正确
			if (kaptchaRecevied == null || !kaptchaRecevied.equals(kaptchaExpected)) {
				request.setAttribute("msg", "验证码错误");
				request.setAttribute("status", Constant.ERROR_CODE_VERICATION_CODE_ERROR);
				return "error";// 返回验证码错误
			}

		}

		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
		try {
			subject.login(token);
		} catch (AuthenticationException e) {
			log.warn("登陆失败");
			request.setAttribute("status", Constant.ERROR_CODE_USERNAME_PASSWORD_MISMATCH);
			request.setAttribute("msg", "账号或密码错误");
			return "error";
		}
		return "redirect:/";
	}
	@ResponseBody
	@RequestMapping(value="/restpassword" , method={RequestMethod.POST})
	public String setPassword(HttpServletRequest request){
		String newPassword = request.getParameter("password");
		String oldPassword = request.getParameter("oldpassword");
		ShiroUser shiroUser = userMapper.findById(Userinfo.getUser().getId()).get();
		if (!shiroUser.getPassword().equals(MD5Util.encrypt(oldPassword)))return "3";
		shiroUser.setPassword(MD5Util.encrypt(newPassword));
		ShiroUser save = userMapper.save(shiroUser);
		//操作成功
		return "1";
	}

	@RequestMapping(value = "/logout" , method = {RequestMethod.GET})
	public String logout(HttpServletRequest request){

		try {
			int id = Userinfo.getUser().getId();
			String username = Userinfo.getUsername();
			String ip = request.getRemoteHost();
			SecurityUtils.getSubject().logout();
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("用户登出");
		return "redirect:/";
	}

}
